package com.jtbd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jtbd.constants.ResponseCodeEnum;
import com.jtbd.constants.StatusConstant;
import com.jtbd.domain.UserEmployee;
import com.jtbd.domain.UserInfo;
import com.jtbd.dto.db.UserEmployeeInfo;
import com.jtbd.exceptions.SystemException;
import com.jtbd.mapper.UserEmployeeMapper;
import com.jtbd.service.UserEmployeeService;
import com.jtbd.service.UserInfoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserEmployeeServiceImpl implements UserEmployeeService {
    @Resource
    private UserEmployeeMapper userEmployeeMapper;
    @Resource
    private UserInfoService userInfoService;

    /**
     * 根据ID获取用户员工信息
     * @param id 用户员工ID
     * @return 用户员工信息对象，包含员工基本信息和用户详细信息；如果未找到对应记录则返回null
     */
    @Override
    public UserEmployeeInfo getUserEmployeeInfoById(Long id) {
        // 查询用户员工基本信息
        UserEmployee userEmployee = userEmployeeMapper.selectById(id);
        if (!ObjectUtils.isEmpty(userEmployee)) {
            // 获取用户详细信息并组装完整用户员工信息
            UserInfo userInfo = userInfoService.getUserInfo(userEmployee.getUid());
            return new UserEmployeeInfo(userEmployee, userInfo);
        }
        return null;
    }


    /**
     * 根据用户ID获取用户员工信息
     *
     * @param uid         用户ID
     * @param hasPassword 是否包含密码信息 true:包含 false:不包含
     * @return 用户员工信息列表，如果用户不存在则返回null
     */
    @Override
    public List<UserEmployeeInfo> getUserEmployeeInfoByUid(Long uid, boolean hasPassword) {
        // 获取用户基本信息
        UserInfo userInfo = userInfoService.getUserInfo(uid);
        if (!ObjectUtils.isEmpty(userInfo)) {
            // 检查账户是否被禁用
            if (userInfo.getStatus() == StatusConstant.BAN) {
                throw new SystemException(ResponseCodeEnum.ERROR_ACCOUNT_BAN.getCode(), ResponseCodeEnum.ERROR_ACCOUNT_BAN.getMsg());
            }
            // 根据参数决定是否清除密码信息
            if (!hasPassword) userInfo.setPassword(null);
            // 查询用户员工信息
            LambdaQueryWrapper<UserEmployee> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserEmployee::getUid, uid);
            List<UserEmployee> userEmployees = userEmployeeMapper.selectList(queryWrapper);
            // 将用户基本信息和员工信息组合返回
            return userEmployees.stream().map(userEmployee -> {
                UserEmployeeInfo userEmployeeInfo = new UserEmployeeInfo();
                userEmployeeInfo.setUserInfo(userInfo);
                userEmployeeInfo.setUserEmployee(userEmployee);
                return userEmployeeInfo;
            }).collect(Collectors.toList());
        }
        return null;
    }


    @Override
    public List<UserEmployeeInfo> getUserEmployeeInfoByUid(Long uid) {
        return getUserEmployeeInfoByUid(uid, false);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int addUserEmployee(UserEmployeeInfo userEmployeeInfo) {
        Long userAddResult = userInfoService.addUserInfo(userEmployeeInfo.getUserInfo());
        if (!ObjectUtils.isEmpty(userAddResult)) {
            userEmployeeInfo.getUserEmployee().setUid(userAddResult);
            return userEmployeeMapper.insert(userEmployeeInfo.getUserEmployee());
        }
        return 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int deleteUserEmployee(Long employeeId) {
//userEmployeeMapper.
        return 0;
    }
}
